package com.pp.tourism.web.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.pp.tourism.web.vo.UserCommentVO;
import com.pp.tourism.web.dto.ApiResponse;
import com.pp.tourism.web.pojo.*;
import com.pp.tourism.web.service.ScenicRecommendService;
import com.pp.tourism.web.service.ScenicSpotService;
import com.pp.tourism.web.service.UserCommentTopService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 景点信息表 前端控制器
 * </p>
 *
 * @author wjm
 * @since 2019-03-20
 */
@RestController
@RequestMapping("/scenicSpot")
public class ScenicSpotController {
    @Autowired
    private ScenicSpotService scenicSpotService;
    @Autowired
    private UserCommentTopService userCommentTopService;
    @Autowired
    private ScenicRecommendService scenicRecommendService;

    @ApiOperation("景点详细信息接口")
    @PutMapping("/queryScenicSpotInfo")
    public ApiResponse queryScenicSpotInfo(@RequestBody Map<String,Object> param,@SessionAttribute("USERNAME") User user){
        try {   //scenicId
            Integer scenicId= MapUtils.getInteger(param, "scenicId");
            Assert.notNull(scenicId,"景点id不能为空");
            //查询景点的详细信息
            ScenicSpot scenicSpot = scenicSpotService.selectById(scenicId);
            //查询景点的点赞次数
            int count = scenicRecommendService.selectCount(new EntityWrapper<ScenicRecommend>().eq("scenic_id", scenicId));
            //查询景点的所有点评 + 评论
//            List<UserCommentTop> userCommentTopsuserCommentTops = userCommentTopService.selectList(new EntityWrapper<UserCommentTop>().eq("scenic_id", scenicId));
            List<UserCommentVO> comments = userCommentTopService.queryComments(scenicId);
            //查询该用户是否点赞过该景点
            boolean recommandFlag = false ;
            if (user != null) {
                int i = scenicRecommendService.selectCount(new EntityWrapper<ScenicRecommend>().eq("user_id", user.getId()).eq("scenic_id", scenicId));
                recommandFlag = i>0?true:false;
            }

            Map<String, Object> result = new HashMap<>();
            result.put("scenicSpot", scenicSpot);
            result.put("count", count);//点赞次数
            result.put("comments", comments);//所有点评 + 评论
            result.put("recommandFlag", recommandFlag);//true是点赞过
            return ApiResponse.ok(result);
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return ApiResponse.error("500", e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return ApiResponse.error("500", "查询失败！");
        }
    }

    /**
     * wjm
     *  查询景点信息
     */
    @ApiOperation("查询景点信息表")
    @PostMapping("/querySport")
    public ApiResponse querySport(@RequestBody String id){
        try {
            /*System.out.println(id);*/
            //查询景点的点赞次数
            Integer count = scenicRecommendService.selectCount(new EntityWrapper<ScenicRecommend>().eq("scenic_id", id));
            //查询景点的所有点评
            List<UserCommentTop> userCommentTops = userCommentTopService.selectList(new EntityWrapper<UserCommentTop>().eq("scenic_id", id));
            //查询景点信息
            ScenicSpot scenicSpot = scenicSpotService.selectById(id);

            scenicSpot.setCreateBy(count);
            scenicSpot.setUpdateBy(userCommentTops.size());
            return ApiResponse.ok(scenicSpot);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResponse.error("500", "查询失败！");
        }
    }

    /**
     * wjm
     * 景点推荐指数+评论数
     * @return
     */
    @PostMapping("/queryAll")
    public ApiResponse queryAllUsers(){
        try {
            List<ScenicRecommend> scenicSpots = scenicSpotService.queryHitSpot();
            List<ScenicSpot1> scenicSpots1 = scenicSpotService.queryAll();
            List<UserCommentTop> qmView = scenicSpotService.queryMaxView();

            ArrayList<ScenicSpot1> scenicSpots2 = new ArrayList<>();
            //基本信息
            for(int i=0;i<scenicSpots.size();i++){
                for (ScenicSpot1 ss:scenicSpots1) {
                    if(scenicSpots.get(i).getScenicId().equals(ss.getId())){
                        scenicSpots2.add(ss);
                    }
                    /*System.out.println(scenicSpots.get(i).getScenicId()+":"+ss.getId());*/
                }
            }
            //评论数
            for(int i=0;i<scenicSpots.size();i++){
                for (ScenicSpot1 ss:scenicSpots1) {
                    if(qmView.get(i).getScenicId().equals(ss.getId())){
                        scenicSpots2.get(i).setView(qmView.get(i).getComment());
                    }
                }
                /*System.out.println(qmView.get(i));*/
            }

            return ApiResponse.ok(scenicSpots2);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResponse.error("500", "查询失败！");
        }
    }

    /**
     * wjm
     * 景点评论最新时间+评论数
     * @return
     */
    @PostMapping("/queryNewView")
    public ApiResponse queryNewView(){
        try {
            List<ScenicSpot1> scenicSpots1 = scenicSpotService.queryAll();

            List<ScenicRecommend> scenicSpots = scenicSpotService.queryHitSpot();
            List<UserCommentTop> qmView = scenicSpotService.queryMaxView();

            ArrayList<ScenicSpot1> scenicSpots2 = new ArrayList<>();
            //基本信息
            for (ScenicSpot1 ss:scenicSpots1) {
                scenicSpots2.add(ss);
            }
            //评论数
            for(int i=0;i<scenicSpots.size();i++){
                for (ScenicSpot1 ss:scenicSpots1) {
                    if(qmView.get(i).getScenicId().equals(ss.getId())){
                        scenicSpots2.get(i).setView(qmView.get(i).getComment());
                    }
                }
            }
            return ApiResponse.ok(scenicSpots2);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResponse.error("500", "查询失败！");
        }
    }
}

